package com.app.service.Impl;

import com.app.pojo.PublicAnnouncement;
import com.app.mapper.PublicAnnouncementMapper;
import com.app.service.IPublicAnnouncementService;
import com.app.util.Result;
import com.app.vo.AdminPublicAnnouncementVo;
import com.app.vo.PublicAnnouncementVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 信息公告 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2024-07-29
 */
@Service
public class PublicAnnouncementServiceImpl extends ServiceImpl<PublicAnnouncementMapper, PublicAnnouncement> implements IPublicAnnouncementService {

    @Autowired
    private PublicAnnouncementMapper publicAnnouncementMapper;


    public PublicAnnouncementVo getPublicAnnouncementList(Integer page,Integer limit,String type) {
        QueryWrapper<PublicAnnouncement> queryWrapper = new QueryWrapper<>();
        if (type!= null &&!type.equals("")) {
            queryWrapper.eq("type", type);
        }
        queryWrapper.orderByDesc("create_time");
        Page<PublicAnnouncement> page1 = new Page<>(page, limit);
        Page<PublicAnnouncement> pageList = publicAnnouncementMapper.selectPage(page1, queryWrapper);
        List<PublicAnnouncement> communityList = pageList.getRecords();
        for (PublicAnnouncement publicAnnouncement : communityList) {
            LocalDateTime localDateTime = publicAnnouncement.getCreateTime();
            // 创建一个DateTimeFormatter实例，用于格式化日期时间
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String formattedDateTime = localDateTime.format(formatter);
            publicAnnouncement.setCreateTimeStr(formattedDateTime);
        }

        PublicAnnouncementVo paymentRecordsLogVo = new PublicAnnouncementVo();
        paymentRecordsLogVo.setCount((int) pageList.getTotal());
        paymentRecordsLogVo.setList(communityList);
        return paymentRecordsLogVo;
    }

    public PublicAnnouncement getPublicAnnouncementById(Integer id) {
        QueryWrapper<PublicAnnouncement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        PublicAnnouncement publicAnnouncement = publicAnnouncementMapper.selectOne(queryWrapper);
        if (publicAnnouncement.getViewsNumber() != null) {
            Integer viewsnumber = publicAnnouncement.getViewsNumber() + 1;
            publicAnnouncement.setViewsNumber(viewsnumber);
        }
        publicAnnouncementMapper.updateById(publicAnnouncement);
        return publicAnnouncement;
    }

    /**
     * 通知公告列表查询
     * @param type 1公告 2通知
     * @param page
     * @param limit
     * @param title 搜索条件（模糊查询）
     * @return
     */
    @Override
    public Result<AdminPublicAnnouncementVo> noticeList(Integer type, Integer page, Integer limit, String title) {
        Page p = new Page(page, limit);
        IPage<PublicAnnouncement> noticeList = publicAnnouncementMapper.noticeList(p,title,type);
        AdminPublicAnnouncementVo adminNoticeVo = new AdminPublicAnnouncementVo();
        adminNoticeVo.setCount((int) noticeList.getTotal());
        adminNoticeVo.setList(noticeList.getRecords());
        return Result.success(adminNoticeVo);
    }

    @Override
    public Result<Boolean> addAndUpdate(PublicAnnouncement publicAnnouncement, Integer userId) {
        if (publicAnnouncement.getId()==null){//新增
            if (publicAnnouncement.getType()!=1 && publicAnnouncement.getType()!=2){
                return Result.error("参数错误   type不能为空（1公告 2通知）");
            }
            publicAnnouncement.setCreateUser(userId);
            publicAnnouncement.setCreateTime(LocalDateTime.now());
            publicAnnouncement.setStatus(1);
            publicAnnouncement.setViewsNumber(0);
            publicAnnouncement.setUpdateTime(LocalDateTime.now());
            publicAnnouncement.setUpdateUser(userId);
            publicAnnouncementMapper.insert(publicAnnouncement);
            return Result.success("新增成功");
        }else {
            publicAnnouncement.setUpdateTime(LocalDateTime.now());
            publicAnnouncement.setUpdateUser(userId);
            publicAnnouncementMapper.updateById(publicAnnouncement);
            return Result.success("修改成功");
        }
    }

}
